import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    // login
    path: '/login', // 默认子路由,只能有一个
    name: 'login',
    component: () => import('@/views/login')
  },
  {
    path: '/',
    // name: 'home',
    component: () => import('@/views/layout'),
    children: [
      {
        // 首页
        path: '', // 默认子路由,只能有一个
        name: 'home',
        component: () => import('@/views/home')
      },
      {
        // 个人信息
        path: '/user', // 默认子路由,只能有一个
        name: 'user',
        component: () => import('@/views/user')
      },

      // 店铺管理
      {
        // 店铺列表
        path: '/list-shop',
        name: 'list-shop',
        component: () => import('@/views/shop/list-shop')
      },
      {
        // 商品详情
        path: '/list-shop/shop-details',
        name: 'shop-details',
        component: () => import('@/views/shop/shop-details')
      },
      {
        // 经营分析
        path: '/list-shop/business-analysis',
        name: 'business-analysis',
        component: () => import('@/views/shop/business-analysis')
      },
      {
        // 店铺权益
        path: '/shop-jurisdiction',
        name: 'shop-jurisdiction',
        component: () => import('@/views/shop/shop-jurisdiction')
      },
      {
        // 店铺行业管理
        path: '/shop-industry',
        name: 'shop-industry',
        component: () => import('@/views/shop/shop-industry')
      },

      // 商品管理
      {
        // 商品列表
        path: '/product-list', // 默认子路由,只能有一个
        name: 'CommodityIndex',
        component: () => import('@/views/commodity/product-list')
      },
      {
        // 商品类别
        path: '/category', // 默认子路由,只能有一个
        name: 'categoryIndex',
        component: () => import('@/views/commodity/commodity-category')
      },
      {
        // 子类别
        path: '/management', // 默认子路由,只能有一个
        name: 'childIndex',
        component: () => import('@/views/commodity/subcategory-list')
      },

      // 订单管理
      {
        // 订单管理
        path: '/order-list',
        name: 'order-list',
        component: () => import('@/views/order/order-list')
      },
      {
        path: '/order-statistics',
        name: 'order-statistics',
        component: () => import('@/views/order/order-statistics')
      },

      // 优惠券管理
      {
        // 通用优惠券
        path: '/discount',
        name: 'discount',
        component: () => import('@/views/discount')
      },

      // 活动
      {
        // 通用优惠券
        path: '/activity',
        name: 'activity',
        component: () => import('@/views/activity')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数,表示放行
  //   next()  放行     next('/login') 强制跳转

  // 判断用户是不是访问登录页,是就直接放行
  if (to.path === '/login') return next()
  // 如果用户访问的不是登录页,那么获取token来判断
  const tokenStr = window.sessionStorage.getItem('token')
  // 判断 tokenStr 的真是存在性
  if (!tokenStr) return next('/login')
  next()
})

export default router
